package com.gm.commonlib.network.basenetwork.numberformat;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.youth.banner.util.LogUtils;

import java.io.IOException;

public class IntegerDefault0Adapter extends TypeAdapter<Integer> {

    @Override
    public void write(JsonWriter out, Integer value) throws IOException {
        out.value(value);
    }

    @Override
    public Integer read(JsonReader in) throws IOException {
        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            return 0;
        }

        String result = in.nextString();
        if(result.equals("") || result.equals("null")) {
            return 0;
        }
        try {
            return Integer.parseInt(result);
        } catch (Exception e) {
            LogUtils.d(e.toString());
            try {
                return (int)Double.parseDouble(result);
            } catch (Exception e1) {
                return 0;
            }
        }
    }
}
